using System.Collections.Generic;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace MingleReport
{
    public class MingleExcel
    {
        private readonly Worksheet worksheet;
        private static readonly int DATA_START_ROW = 2;

      
        public MingleExcel(IMingleSession session)
        {
            worksheet = (Worksheet)session.CurrentWorkbook.ActiveSheet;
        }

        public void Export(List<StoryCard> cards)
        {
            setCardColumns();
            int currentRow = DATA_START_ROW;
            foreach (StoryCard card in cards)
            {
                worksheet.Cells[currentRow, 1] = card.Number;
                worksheet.Cells[currentRow, 2] = card.Name;
                worksheet.Cells[currentRow, 3] = card.Description;
                worksheet.Cells[currentRow++, 4] = card.Type;
            }
            autoFitColumns();
        }

        public int GetLastUsedRow()
        {
            return worksheet.Cells.Find("*", Missing.Value, Missing.Value,Missing.Value,
                XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious, false, Missing.Value,Missing.Value).Row;
        }

        public int GetLastUsedColumn()
        {
            return worksheet.UsedRange.Columns.Count;
        }

        private void autoFitColumns()
        {
            worksheet.Columns.AutoFit();
        }

        private void setCardColumns()
        {
            worksheet.Cells[1, 1] = "Number";
            worksheet.Cells[1, 2] = "Name";
            worksheet.Cells[1, 3] = "Description";
            worksheet.Cells[1, 4] = "Type";  
        }
    }
}
